package com.netted.operationassistant.activity;

//public class DetectionFinishActivity extends BaseActivity  implements QRCodeView.Delegate{
public class DetectionFinishActivity extends BaseActivity  {
    /*private ZXingView mZXingView;
    private String params = "";
    private JSONObject paramsJson = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detection_finish);
        mZXingView = findViewById(R.id.detectionFinish);
        mZXingView.setDelegate(this);

        params = getIntent().getStringExtra("params");
        if (params == null || "".equals(params)){
            showDialog("出错啦", "非正常进入", new SuperDialog.OnClickPositiveListener() {
                @Override
                public void onClick(View v) {
                    finishActivity();
                }
            });
        }else{
            paramsJson = JSONObject.parseObject(params);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
//        mZXingView.startCamera(0); // 打开后置摄像头开始预览，但是并未开始识别
        mZXingView.startCamera(1); // 打开前置摄像头开始预览，但是并未开始识别

        mZXingView.startSpotAndShowRect(); // 显示扫描框，并且延迟0.5秒后开始识别
    }

    public void exitActivity(View view){
        this.finishActivity();
    }

    @Override
    public void onScanQRCodeSuccess(String res) {

        mZXingView.startSpot(); // 延迟0.5秒后开始识别
        *//**
         * 平板设备生成二维码,必须包含以下信息
         * catbinetId:机柜id
         * token:取机码
         * time:时间按戳,必须与之前扫描机柜的时间戳一致
         * result: 取值success或fail,表示检测结果是否通过
         * port:仓门名称
         *//*


        if (res != null){
            JSONObject json = JSONObject.parseObject(res);


            LogUtils.e("params############:"+params);
            LogUtils.e("result############:"+res);
            String timeTag = paramsJson.getString("time");
            String time = json.getString("time");

            String catbinetIdTag = paramsJson.getString("catbinetId");
            String catbinetId = json.getString("catbinetId");

            String tokenTag = paramsJson.getString("token");
            String token = json.getString("token");
            String portTag = paramsJson.getString("port");
            final String port = json.getString("port");


            if (portTag == null || !portTag.equals(port)){
                showDialog("提示", "无效平板", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finishActivity();
                    }
                });
                return;
            }
            if (timeTag == null || !timeTag.equals(time)){
                showDialog("提示", "无效平板", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finishActivity();
                    }
                });
                return;
            }

            if (catbinetIdTag == null || !catbinetIdTag.equals(catbinetId)){
                showDialog("提示", "无效平板", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finishActivity();
                    }
                });
                return;
            }

            if (tokenTag == null || !tokenTag.equals(token)){
                showDialog("提示", "无效平板", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finishActivity();
                    }
                });
                return;
            }

            if (Long.parseLong(time) - System.currentTimeMillis() > (1000*60*10)){
                showDialog("提示", "操作超时,请重新检测", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finishActivity();
                    }
                });
                return;
            }

            String result = json.getString("result");
            if ("success".equals(result)){
                try {
                    openPort(port);
                    Thread.sleep(1000);
                    if (!"".equals(serialValue)){
                        final ProgressDialog loadingDialog = ProgressDialog.show(DetectionFinishActivity.this,"提示", "请在30秒内江平板放入仓内并关闭仓门", true, false);
                        loadingDialog.show();
                        int i = 0;

                        while (i < 10){
                            if (checkDoor(port)){
                                loadingDialog.dismiss();
                                ToastUtils.showLong("您已成功归还平板,感谢您的使用");
                                finishActivity();
                                return;
                            }
                            i++;
                            Thread.sleep(1000);
                        }
                        loadingDialog.dismiss();
                        ToastUtils.showLong("归还平板超时");
                        finishActivity();

                    }else{
                        showDialog("提示", "仓门打开失败,请重新检测", new SuperDialog.OnClickPositiveListener() {
                            @Override
                            public void onClick(View v) {
                                finishActivity();
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else{
                showDialog("提示", "您的平板未能通过检测,请重新检测或通过家长小程序提交报修流程", new SuperDialog.OnClickPositiveListener() {
                    @Override
                    public void onClick(View v) {
                        finishActivity();
                    }
                });
                return;
            }

        }else{
            ToastUtils.showLong("二维码错误");
            finishActivity();
        }
    }


    public boolean checkDoor(String port){
        try {
            getPortStatus(port);
            Thread.sleep(1000);
            if ("".equals(serialValue)){
                return false;
            }else{
                Port p = hexToPort(serialValue);
                //如果设备不在仓内或者仓门没关闭,则返回false
                if (!"1".equals(p.getIsHas()) || !"1".equals(p.getIsOpen())){
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
            return false;
        }
        return true;
    }

    public void finishActivity(){
//        mZXingView.stopCamera();
        try{
            Intent intent = new Intent(this,MenuActivity.class);
            startActivity(intent);
            this.finish();
        }catch (Exception e){
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }


    }

    *//**
     * 打开仓门
     *//*
    public void openDoor(String catbinetId,String token, String params){
        Map<String,String> paramMap = new HashMap<>();
        paramMap.put("catbinetId",catbinetId);
        paramMap.put("token",token);
        paramMap.put("params",params);
        String url = Constant.HOST+"/ws/opt/put";
        request.post(url, paramMap, "post", new HttpRequest.Callback() {
            @Override
            public void onSuccess(String response) {
                if (!StringUtils.isEmpty(response)){
                    JSONObject resJson = JSONObject.parseObject(response);
                    if (!"success".equals(resJson.getString("flag"))){
                        showDialog("提示",resJson.getString("data")+"仓门已打开，请归还平板后关闭仓门",null);
                    }
                }

            }

            @Override
            public void onError(VolleyError error) {

            }
        });
    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }

    @Override
    protected void onStop() {
        super.onStop();

    }*/
}
